package user_info

import (
	"encoding/json"
	"gitlab.gaodun.com/golib/gderror"
	"strconv"
	"permission/api/param_type"
	"permission/config"
	"permission/vigo_loger"
	"permission/vigo_tool"
)

type VigoCache struct {
	cache        vigo_tool.CacheInterface
	Debug        bool
	DelCacheList []string
}

const (
	VIGO_NAVIGATION          = "_getNavigations"
	VIGO_FUNCTION            = "_getFunctions"
	VIGO_FUNC_NAV            = "_getFuncNav"
	VIGO_ROLES               = "_getRoles"
	VIGO_USER                = "_getUser"
	VIGO_DEPARTMENT          = "_getDepartmentd"
	VIGO_DEPARTMENT_ALL      = "_getDepartmentdAll"
	VIGO_DEPARTMENT_ALL_U    = "_getDepartmentdAllUser"
	VIGO_DEPARTMENT_U        = "_getDepartmentu"
	VIGO_DEPARTMENT_U_ALL    = "_getDepartmentuUAll"
	VIGO_DEPARTLEADER        = "_getleader"
	VIGO_NAVIGATION_NODE     = "_getNavigationNode"
	VIGO_DEPART_USERS        = "_getDepartUsers"
	VIGO_USERS_ALL           = "_getUsersAll"
	VIGO_DEPARTMENT_USER     = "_getDepartmentduser"
	VIGO_DEPARTMENT_USER_MAP = "_getDepartmentdusermap"
	VIGO_ROLE_DEPART         = "_getRoleDepart"
	VIGO_USER_DEPART_ALL     = "_getUserDepartAll"
)

type CacheKeys struct {
	List []string
}

func (ck *CacheKeys) Add(key string) {
	for _, val := range ck.List {
		if val == key {
			return
		}
	}
	ck.List = append(ck.List, key)
}

var VigoKeys CacheKeys

// 返回
func NewVigoCache() *VigoCache {
	var vigoCah VigoCache
	vigoCah.cache = vigo_tool.NewRedisFactory()
	vigoCah.Debug = config.VigoDebug
	return &vigoCah
}

func (vc *VigoCache) Close() {
	return
	err := vc.cache.Close()
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
	}
}

// 注销用户登陆
func (vc *VigoCache) DestroyUser(userId string) (bool, error) {
	_, err := vc.cache.Del("login_"+userId)
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
		return false, err
	}
	return true, nil
}

func (vc *VigoCache) CheckLogin(userId int32) interface{} {
	t := vc.Debug
	vc.Debug = false
	strUserId:=strconv.Itoa(int(userId))
	key:="login_"+ strUserId
	val := vc.GetVigoInfoCache(key)
	vc.Debug = t
	return val
}

// 设置用户缓存
func (vc *VigoCache) SetUserInfoCache(userInfo param_type.ApiUserInfoResponse, appId int32, gdSid string) error {
	userIdStr := strconv.FormatInt(userInfo.UserID, 10)
	keyUserStr := userIdStr + VIGO_USER
	gdSidStr := gdSid
	// 存入缓存
	err := vc.SetVigoInfoCache(keyUserStr, userInfo)
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
	}
	err = vc.SetVigoInfoCache(gdSidStr, userInfo)
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
	}
	return err
}

func (vc *VigoCache) SetVigoInfoCacheLog(key string, data interface{}) error {
	val, err := json.Marshal(&data)
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
		return err
	}
	return vc.cache.Set(key, val)
}

// 设置存缓存
func (vc *VigoCache) SetVigoInfoCache(key string, data interface{}) error {
	if vc.Debug == true {
		return nil
	}
	val, err := json.Marshal(&data)
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
		return err
	}
	return vc.cache.Set(key, val)
}

// 取缓存
func (vc *VigoCache) GetVigoInfoCache(key string) interface{} {
	if vc.Debug == true {
		return nil
	}
	data, err := vc.cache.Get(key)
	if err != nil {
		vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
	}
	return data
}

func (vc *VigoCache) SetExpire(expireTime int64) {
	vc.cache.SetExpire(expireTime)
}

func (vc *VigoCache) GetExpire() int64 {
	return vc.cache.GetExpire()
}

// 删除列表
func (vc *VigoCache) AddDelCacheList(key string) {
	vc.DelCacheList = append(vc.DelCacheList, key)
}

// 删除所有缓存
func (vc *VigoCache) DelAllCache() {
	for _, val := range vc.DelCacheList {
		_, err := vc.cache.Del(val)
		if err != nil {
			vigo_loger.AddGrayLog(gderror.New(err.Error()).Error())
		}
	}
}
